package com.rockcent.job.task;

import com.rockcent.helper.SysMessageHelper;
import com.rockcent.mall.lib.constants.SysMessageConstants;
import com.rockcent.mall.lib.domain.MerchantGiftPacket;
import com.rockcent.mall.lib.domain.SysMessage;
import com.rockcent.mall.lib.domain.enumclass.GiftPacketStatus;
import com.rockcent.mall.lib.domain.enumclass.SysMessageType;
import com.rockcent.mall.lib.repository.MerchantGiftPacketRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.text.MessageFormat;
import java.util.List;

/**
 * Created by Marco on 16-11-27.
 * 礼包
 */
@Component
public class GiftPacketTask {

    final Logger log = LoggerFactory.getLogger(GiftPacketTask.class);

    @Autowired
    private MerchantGiftPacketRepository merchantGiftPacketRepository;

    /**
     * 已过期的礼包修改状态为已失效即为已结束
     */
    @Scheduled(cron = "${job.giftPacketTask.cron}")
    public void updateGiftPacketFinishedStatus() {
        //查询已过期礼包列表
        List<MerchantGiftPacket> giftPackets = merchantGiftPacketRepository.getGiftPacketExpired();
        if (giftPackets != null && giftPackets.size() > 0) {
            giftPackets.forEach(giftPacket -> {
                giftPacket.setStatus(GiftPacketStatus.S5_FINISHED);
                merchantGiftPacketRepository.save(giftPacket);

                //广播消息
                try {

                    String content = SysMessageConstants.SYS_GIFT_PACKAGE_EXPIRED.getContent();
                    content = MessageFormat.format(content, giftPacket.getName());

                    String frontedUrl = SysMessageConstants.SYS_GIFT_PACKAGE_EXPIRED.getFrontendUrl();
                    frontedUrl = MessageFormat.format(frontedUrl, giftPacket.getName());
                    SysMessage message = SysMessageHelper.SINGLETON.createMsgBean(SysMessageConstants.SYS_GIFT_PACKAGE_EXPIRED, SysMessageType.OP, content);
                    message.setFrontendUrl(frontedUrl);
                    //广播消息
                    SysMessageHelper.SINGLETON.pushMsgByMallAndMerchantId(message, null, giftPacket.getMerchantId());
                } catch (Exception e) {
                    log.error("updateGiftPacketFinishedStatus sendMessage fail:", e);
                }
            });
//            log.info("======================>处理已过期的礼包数量为：" + giftPackets.size());
        }
    }

    /**
     * 未开始礼包超过了开始时间状态未变成“进行中”状态
     */
    @Scheduled(cron = "${job.giftPacketTask.cron}")
    public void updateGiftPackeSellingStatus() {
        //查询未开始礼包列表
        List<MerchantGiftPacket> giftPackets = merchantGiftPacketRepository.getGiftPacketNotStart();
        if (giftPackets != null && giftPackets.size() > 0) {
            giftPackets.forEach(giftPacket -> {
                giftPacket.setStatus(GiftPacketStatus.S2_SELLING);
                merchantGiftPacketRepository.save(giftPacket);
            });
//            log.info("======================>处理未开始的礼包数量为：" + giftPackets.size());
        }
    }

}
